<div id="banners">
	<div class="show_title">
		<h2>广告列表</h2>
	</div>
    <div id="search_area" class="right">
        <input class="input" name="search_txt" id="search_txt" style="width:200px"/><input class="input" type="button" name="search_btn" value="搜索" />
    </div>
    <div class="clear">
    </div>
    <div class="top_action">
        <select class="select" name="action">
            <option value="0">批量动作</option>
            <option value="/admin/admin_banner/delbanners">删除</option>
        </select>
        <input class="input" type="button" name="do_btn" id="do_btn" value="应用">
        </input>
		<select class="select" id="filter">
        <option value="0">查看全部分类目录</option>
		<foreach name="zones" var="item" key="key">
			<option value="/admin/admin_banner/getBannersByZone?item=[$item.z_id]">[$item.z_name]</option>
		</foreach>
        </select>
        <input type="button" class="input" name="filter_btn" id="filter_btn" value="过滤">
        </input>
        <div class="page right">
            <if $page_info.page.previous_url>
	                <a class='text' href='[@htmlspecialchars($page_info.page.previous_url)]'>上一页</a>
            </if>
            <foreach name="page_info.page.pages" key="key" var="var">
                <if $var.page_num == $page_info.current_page>
                    <span title="第[@htmlspecialchars($var.page_num)]页">[@htmlspecialchars($var.page_num)]</span>
                    <else>
                    <a class="num" href="[@htmlspecialchars($var.page_link)]" title="第[@htmlspecialchars($var.page_num)]页">[@htmlspecialchars($var.page_num)]</a>
                </if>
            </foreach>
            <if $page_info.page.next_url>
                <a class='text' href='[@htmlspecialchars($page_info.page.next_url)]'>下一页</a>
            </if>
        </div>
    </div>
	<if $result.banners>
    <table width="100%" class="fixed table">
        <thead>
            <tr>
                <th width="10%">
                    <input type="checkbox" name="check_all"/>
                </th>
                <th width="23%">
                    广告名称
                </th>
                <th width="23%">
                    所属广告位
                </th>
				<th width="22%">
                    所属项目
                </th>
                <!--<th width="18%">
                   有效日期
                </th>-->
                <th width="22%">
                  状态
                </th>
            </tr>
        </thead>
        <tfoot>
           <tr>
                <th width="10%">
                    <input type="checkbox" name="check_all"/>
                </th>
                <th width="23%">
                    广告名称
                </th>
                <th width="23%">
                   所属 广告位
                </th>
				<th width="22%">
                    所属项目
                </th>
                <!--<th width="18%">
                   有效日期
                </th>-->
                <th width="22%">
                  状态
                </th>
            </tr>
        </tfoot>
        <tbody>
            <foreach name="result.banners" var="banner" key="key">
                <tr class="table_list" >
                    <th>
                        <input type="checkbox" id="[$banner.b_id]" value="[$banner.b_id]"/>
                    </th>
					<td>
						 [@htmlspecialchars($banner.b_name)]
						<div class="row_action" num="[$banner.b_id]">
                            <span class="action_edit"><a href="/admin/admin_banner/editbanner">编辑</a></span> | <span class="action_delete"><a href="javascript:;">删除</a> | <span class="action_preview"><a target="_blank" href="/ads/preview?key=[$banner.b_id]">查看</a></span>
                        </div>
                    </td>
					 <td>
                        [$zones.$banner:b_zone.z_name]
                    </td>
					 <td>
                        [$projects.$banner:b_project.p_name]
                    </td>
                    <td>
                    	<div class="banner_status">
	                        <if $banner.b_status == '1'>
								已发布<input type="button" num="[$banner.b_id]"  zone="[$banner.b_zone]" class="cancel_release" value="撤销">
	                        <else>
	                        	未发布<input type="button" num="[$banner.b_id]" zone="[$banner.b_zone]" class="release" value="发布">
							</if>
                        </div>
                    </td>
                </tr>
            </foreach>
        </tbody>
    </table>
	<else>
		<div class="nothing">没有记录</div>
	</if>
    <div class="top_action">
        <div class="page right">
            <if $page_info.page.previous_url>
                <a class='text' href='[@htmlspecialchars($page_info.page.previous_url)]'>上一页</a>
            </if>
            <foreach name="page_info.page.pages" key="key" var="var">
                <if $var.page_num == $page_info.current_page>
                    <span title="第[@htmlspecialchars($var.page_num)]页">[@htmlspecialchars($var.page_num)]</span>
                    <else>
                    <a class="num" href="[@htmlspecialchars($var.page_link)]" title="第[@htmlspecialchars($var.page_num)]页">[@htmlspecialchars($var.page_num)]</a>
                </if>
            </foreach>
            <if $page_info.page.next_url>
                <a class='text' href='[@htmlspecialchars($page_info.page.next_url)]'>下一页</a>
            </if>
        </div>
    </div>
</div>
<script>
    $(function(){
        $.checkboxEvent();
        
        $("#do_btn").click(function(e){
            $.doBatchAction();
            return false;
        });
		$("#filter_btn").click(function(e){
			var filter = $("#filter").val();
			if(filter == "0")
			{
				$.showPageInfo("#page_info",'请过滤条件',500,true);
			}
			else
			{
				$.getFilter(filter);
			}
			return false;
		});
        
        $.edit();
        $.delObject('/admin/admin_banner/delbanners');
        $("[name=search_btn]").unbind('click').click(function(e){
            var $key = $("#search_txt").val();
            if ($key == "") {
                $.showPageInfo("#page_info", '请填写查询条件', 500, true);
                return false;
            }
            $url = "/admin/admin_banner/searchbanners?keywords=" + $key;
            $.search($url);
            return false;
        });
		$(".cancel_release").die('click').live("click",function(){
			var $this = $(this);
			var $parent= $(this).parent(".banner_status");
			var $id   = $this.attr("num");
			var $zone = $this.attr("zone");
			
			if(confirm("是否撤销当前广告？"))
			{
				$.ajax({
					url:"/admin/admin_banner/unrelease",
					data:{'b_id':$id},
					type:"POST",
					dataType:'json',
					success:function(data){
						if(data.status)
						{
							$parent.html('未发布<input type="button" num="'+$id+'" zone="'+$zone+'" class="release" value="发布">');
						}
						$.showPageInfo("#page_info", data.info, 500, true);
					}
				});
				
			}
			return false;
		});
		$(".release").die("click").live("click",function(){
			var $this = $(this);
			var $parent= $(this).parent(".banner_status");
			var $zone = $this.attr('zone');
			var $id = $this.attr('num');
			$.ajax({
				url:"/admin/admin_banner/hasrelease",
				data:{'zone':$zone},
				type:"POST",
				dataType:"json",
				success:function(data){
					if(data=="1")
					{
						if(confirm("当前广告位已存在广告，是否替换？"))
						{
							release($zone,$id,function(data){
								unrelease(data.unreleased,$zone);
								$parent.html('已发布<input type="button" num="'+$id+'" zone="'+$zone+'" class="cancel_release" value="撤销">');
							});
							
						}
					}
					else
					{
						release($zone,$id,function(){
							$parent.html('已发布<input type="button" num="'+$id+'" zone="'+$zone+'" class="cancel_release" value="撤销">');
						});
					}
				}
			});
			return false;
		});
		
		function release($zone,$id,func){
			
			$.ajax({
				url:"/admin/admin_banner/release",
				data:{'zone':$zone,'b_id':$id},
				type:"POST",
				dataType:"json",
				success:function(data){
					$.showPageInfo("#page_info", data.info, 500, true);
					if(data.status)
					{
						func(data);
					}
				}
			});
		}
		
		function unrelease($arr,$zone){
			$($arr).each(function(index,item){
				var $p = $(".cancel_release[num="+item+"]").parent(".banner_status");
				$p.html('未发布<input type="button" num="'+item+'" zone="'+$zone+'" class="release" value="发布">');
			});
		}
		
        $.initForIe6();
    })
</script>
